package com.rbmh.network.utils;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class DocUtil {

    /**
     * 读取Word文件内容（支持.doc和.docx）
     * @param filePath Word文件路径
     * @return 提取的文本内容（空字符串表示读取失败或无内容）
     */
    public static String readWord(String filePath) {
        StringBuilder content = new StringBuilder();
        // 校验文件路径非空
        if (filePath == null || filePath.trim().isEmpty()) {
            System.err.println("错误：文件路径不能为空");
            return content.toString();
        }

        File file = new File(filePath);
        // 校验文件是否存在且为文件（非文件夹）
        if (!file.exists()) {
            System.err.println("错误：文件不存在，路径：" + filePath);
            return content.toString();
        }
        if (file.isDirectory()) {
            System.err.println("错误：路径指向文件夹，非文件，路径：" + filePath);
            return content.toString();
        }

        // try-with-resources：自动关闭FileInputStream，无需手动调用close()
        try (FileInputStream fis = new FileInputStream(file)) {
            // 区分.doc和.docx格式
            if (filePath.endsWith(".doc")) {
                // 处理.doc文件（依赖poi-scratchpad）
                try (HWPFDocument document = new HWPFDocument(fis)) { // 文档也用try-with-resources管理
                    WordExtractor extractor = new WordExtractor(document);
                    content.append(extractor.getText());
                }
            } else if (filePath.endsWith(".docx")) {
                // 处理.docx文件（依赖poi-ooxml）
                try (XWPFDocument document = new XWPFDocument(fis)) { // 文档也用try-with-resources管理
                    XWPFWordExtractor extractor = new XWPFWordExtractor(document);
                    content.append(extractor.getText());
                }
            } else {
                throw new IllegalArgumentException("不支持的文件格式！仅支持.doc和.docx，当前文件：" + filePath);
            }

        } catch (IllegalArgumentException e) {
            // 捕获文件格式错误
            System.err.println("格式错误：" + e.getMessage());
        } catch (IOException e) {
            // 捕获文件读取IO错误（如权限不足、文件损坏等）
            System.err.println("文件读取失败，路径：" + filePath);
            System.err.println("错误详情：" + e.getMessage());
        }

        return content.toString();
    }

    public static void main(String[] args) {
        // 测试读取.doc文件（替换为你的实际文件路径）
        String docFilePath = "C:\\Users\\Administrator\\Desktop\\XX县人社局工作介绍信某某.doc";
        String docContent = readWord(docFilePath);

        System.out.println("=== Word文件内容 ===");
        System.out.println(docContent.isEmpty() ? "未读取到内容或读取失败" : docContent);

            String filePath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\XX县人社局工作介绍信某某.doc"; // Word文档的路径
            try (FileInputStream fis = new FileInputStream(filePath);
                 XWPFDocument document = new XWPFDocument(fis)) {

                for (XWPFParagraph paragraph : document.getParagraphs()) {
                    System.out.println(paragraph.getText());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }



        /*String path = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\XX县人社局工作介绍信某某.doc";
        String buffer = "";
        try {
            if (path.endsWith(".doc")) {
                FileInputStream is = new FileInputStream(path);
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();
                is.close();
            } else if (path.endsWith("docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
                opcPackage.close();
            } else {
//                return AjaxResult.error("文件不是word文件");
            }
        } catch (Exception e) {
            //e.printStackTrace();
//            return AjaxResult.error("读取word文件失败"+e.getMessage());
        }*/

    }

}
